<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class realisasi_model extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	//get data ke v_realisasi uncle
	
	 function getdata($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'TAHUN';  
		$sort1 = isset($_POST['sort']) ? strval($_POST['sort']) : 'BULAN';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'desc';  
		//$tahun = isset($_POST['tahun']) ? strval($_POST['tahun']) : '(SELECT MAX(TAHUN) FROM LQ_LEGAL_TASK where STATUS_PROSES=4)';  
		
		
		$offset = ($page-1)*$limit;
		
		#get filter
		$nama = isset($_POST['NAMA_KPS']) ? strval($_POST['NAMA_KPS']) : '';	
	
		
		# create query
			$this->db->flush_cache();
			$this->db->start_cache();
			$this->db->select('ID_KPS, NAMA_KPS, TAHUN,BULAN,STATUS_PROSES,BLN');
			$this->db->from('V_REALISASI');
						
			//$query1 = "(SELECT MAX(TAHUN) FROM LQ_LEGAL_TASK)";
			
			$where = "(upper(NAMA_KPS) like upper('%$nama%') or upper (TAHUN) like upper('%$nama%') or upper (BLN) like upper('%$nama%')) ";
			$this->db->where('STATUS_PROSES','3');
			$this->db->where($where);
			
		
		$this->db->group_by('ID_KPS, NAMA_KPS, TAHUN,BULAN,STATUS_PROSES,BLN');
		$this->db->order_by($sort, $order);
		$this->db->order_by($sort1, $order);
		
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	//Get data detail ke v_realisasi
	function getdata_detail($id, $tahun,$bulan){
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'NM_PROD';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		
				
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('ID_KPS, NAMA_KPS, CD_PROD, JENIS, SORTORDER, NM_PROD,
							   ID_WIL, WILAYAH, TAHUN, BULAN, BAREL, MMBTU, MSCF,MT, 
							   ICP,VOL_PREMIUM ,PREMIUM_PRICE,GROSSREV,NETBACK ,REMARK, DELSTAT, STATUS_PROSES');
			$this->db->from('V_REALISASI');
			$this->db->where('ID_KPS', $id);
			$this->db->where('TAHUN', $tahun);
			$this->db->where('BULAN', $bulan);
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	function togrid($data, $count){
		$response->total = $count;
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
		}else{
		$response->rows = array();
		}
		return json_encode($response);
	}
	
	function UpdateOnDb($data){
		$this->db->flush_cache();
		$this->db->set('STATUS_PROSES', '4');
		
		
		//$this->db->set('U_USER', $this->session->userdata('USERID'));
		//$this->db->set('I_DATE', date('Y-m-d H:i:s'));
		// $this->db->set('', $data['']);
		// $this->db->set('', $data['']);
		
		$this->db->where('ID_KPS', 	$data['ID_KPS']);
		$this->db->where('TAHUN', 	$data['TAHUN']);
		$this->db->where('BULAN', 	$data['BULAN']);
		
		return $this->db->update('LQ_LEGAL_TASK');
		
	}
	function getdataedit($id,$bulan){
		$this->db->flush_cache();
		$this->db->select('*');
	
		$this->db->from('V_REALISASI');
		$this->db->where('ID_KPS', $id);
		$this->db->where('BULAN', $bulan);
		
		return $this->db->get();
	}
}

?>